/*******************************************************************************
				TABLE 4
*******************************************************************************/

*WAGE IV, COWORKERS
*TABLE 4, PANEL A, DYNAMICS
cap{
clear 
use "$data/base WAGE IV companies.dta", clear

********************************************************************************

global xi edad edad2 sexo anos_educ puntaje lnw
global xg m_l_edad m_l_sexo m_l_puntaje m_l_anos_educ

local dep l_net
local iv z_lnet
}
foreach year of numlist 2010/2012{
cap{
eststo clear

*1. FIRST STAGE:
qui reg `dep' $xi $xg `iv', cluster(rut_emp)
	eststo first_lnet

*2. REDUCED FORM:
qui reg sej`year' $xi $xg `iv', cluster(rut_emp)
	eststo rf_lnet
	
*3. SECOND STAGE:
qui ivreg sej`year' $xi $xg (`dep' = `iv'), cluster(rut_emp)
	eststo iv_lnet
}
esttab first_lnet rf_lnet iv_lnet, keep(`dep' `iv') ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations)) brackets ///
	coeflabels(l_net "Endogenous" z_lnet "Instrument") ///
	title("Table 4 Panel A. Wage Instrument: Evolution of peer effects over time, coworkers' network. `year'")
}
clear

********************************************************************************

*AGE IV, COWORKERS
*TABLE 4, PANEL B: DYNAMICS.
cap{
use "$data/base AGE IV.dta", clear

global xi edad edad2 sexo lnw anos_educ puntaje
global xg m_l_sexo m_l_lnw m_l_anos_educ m_l_puntaje 

local dep lnet
local iv zlnet
}
foreach year of numlist 2010/2012{
cap{
eststo clear
*1. FIRST STAGE:
qui reg lnet $xi $xg `iv', cluster(rut_emp)
	eststo first_firm

*2. REDUCED FORM:
qui reg sej`year' $xi $xg `iv' , cluster(rut_emp)
	eststo rf_firm

*3. SECOND STAGE:
qui ivreg sej`year' $xi $xg (`dep' = `iv'), cluster(rut_emp)
	eststo iv_firm
}
esttab first_firm rf_firm iv_firm, ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations F))  ///
	brackets coeflabels(`dep' "Endogenous" `iv' "Instrument") ///
	keep(`dep' `iv') mtitles("First Stage" "Reduced Form" "TSLS") ///
	title("Table 4 Panel B. Age Instrument: Evolution of peer effects over time, coworkers' network. `year'")
}
clear
